/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Report4.java
 *
 * Created on Feb 26, 2013, 8:28:40 PM
 */
package membershipdemo;

import java.awt.Desktop;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.util.JRProperties;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

/**
 *
 * @author $alama
 */
public class Report4 extends javax.swing.JFrame {

    private Query query;
    private EntityManager entityManager;
    private MemberShipDemoView memberShipDemoView;
    /** Creates new form Report4 */
    public Report4(MemberShipDemoView memberShipDemoView) {
        this.memberShipDemoView=memberShipDemoView;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(membershipdemo.MemberShipDemoApp.class).getContext().getResourceMap(Report4.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(87, 87, 87)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(161, 161, 161)
                        .addComponent(jButton1)))
                .addContainerGap(115, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(61, 61, 61)
                .addComponent(jLabel1)
                .addGap(86, 86, 86)
                .addComponent(jButton1)
                .addContainerGap(113, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        generate();        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    this.memberShipDemoView.enable_Report_4_Button();
}//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new Report4().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

    public void generate() {
        try {

            org.jdesktop.application.ResourceMap resourceMap1 = org.jdesktop.application.Application.getInstance(membershipdemo.MemberShipDemoApp.class).getContext().getResourceMap(MemberShipDemoView.class);

            entityManager = javax.persistence.Persistence.createEntityManagerFactory(resourceMap1.getString("entityManager.persistenceUnit")).createEntityManager();

            query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery(resourceMap1.getString("query.query")); // NOI18N
            entityManager.getTransaction().begin();
            java.util.Collection data = query.getResultList();

            Map parameters = new HashMap();
            parameters.put("image", "ac.jpg");
            FileInputStream inputStream = new FileInputStream("report4.jrxml");
            JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            ArrayList<Report4Bean> memberses = new ArrayList<Report4Bean>();
            int i = 0;
            for (Iterator it = data.iterator(); it.hasNext();) {
                Members object = (Members) it.next();
                Date d = object.getDateOfBirth();
                int age = new Date().getYear() - d.getYear();
                System.out.println("   " + age);
                if (age >= 60) {

                    memberses.add(new Report4Bean(object.getId(), object.getMemId(), object.getMemName(), new SimpleDateFormat("yyyy/MM/dd").format(object.getDateOfBirth()), age + ""));
                    i++;
                }
            }
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JRBeanArrayDataSource(memberses.toArray()));
            JRHtmlExporter jRPdfExporter = new JRHtmlExporter();
            jRPdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

            JRProperties.setProperty("net.sf.jasperreports.default.pdf.encoding", "UTF-8");

            jRPdfExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");

            jRPdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
                    "report3.html");
            jRPdfExporter.exportReport();
            Desktop.getDesktop().open(new File("report3.html"));
        } catch (JRException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    private boolean isUp60(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, 60);
        return c.before(new Date());
    }
}
